package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LeasesGUI extends JFrame implements ActionListener{
    private Container container;
    private JPanel panelLeases;
    private JPanel panelShowLease;
    private JPanel panelShowLease2;
    private JPanel panelStartLease;
    private JPanel panelFinishLease;
    private JPanel panelDeleteLease;
    private JPanel panelDeleteLease2;
    JLabel labelProduct;
    JLabel errorMessage;
    JTextField textBarcode;
    JTextField textQuantity;
    private int count;
    private int sessionId;
    private int leaseId;
    private LeaseController leaseController;

    public LeasesGUI(int sessionId){
        panelLeases = new JPanel(new GridBagLayout());
        panelStartLease = new JPanel(new GridBagLayout());
        panelFinishLease = new JPanel(new GridBagLayout());
        panelShowLease = new JPanel(new GridBagLayout());
        panelShowLease2 = new JPanel(new GridBagLayout());
        panelDeleteLease = new JPanel(new GridBagLayout());
        panelDeleteLease2 = new JPanel(new GridBagLayout());
        leaseController = new LeaseController();

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
//         this.setResizable(false);
        count = 0;
        leaseId = 0;
        this.sessionId = sessionId;
        menuLeases();
    }

    public void actionPerformed(ActionEvent e){

    }

    public void makeMenuLeases(){
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addLease = new JButton("Start a Lease");
        addLease.setPreferredSize(new Dimension(150, 25));
        addLease.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    startLease();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelLeases.add(addLease, gbc);

        JButton finishLeases = new JButton("Finish a Lease");
        finishLeases.setPreferredSize(new Dimension(150, 25));
        finishLeases.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    finishLease();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelLeases.add(finishLeases, gbc);

        JButton showLeases = new JButton("Show a Lease");
        showLeases.setPreferredSize(new Dimension(150, 25));
        showLeases.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findLease();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelLeases.add(showLeases, gbc);

        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton deleteLease = new JButton("Delete a Lease");
            deleteLease.setPreferredSize(new Dimension(150, 25));
            deleteLease.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteLeaseFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(5,5,5,5);
            panelLeases.add(deleteLease, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose(); 
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelLeases.add(back, gbc);
    }

    public void menuLeases(){
        makeMenuLeases();
        this.getContentPane().add(panelLeases, BorderLayout.NORTH);
    }

    private void menuStartLease(){
        JTextField textPeriod = new JTextField();
        JTextField textCustomerId = new JTextField();

        JLabel labelPeriod;
        JLabel labelCustomerId;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JButton buttonAdd;
        JButton buttonRemove;
        JButton buttonSubmit;
        JButton buttonClear;
        JButton buttonBack;
        GridBagConstraints gbc = new GridBagConstraints();

        labelPeriod = new JLabel("Period:", JLabel.RIGHT);
        labelPeriod.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(labelPeriod, gbc);

        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textPeriod.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(textPeriod,gbc);

        labelCustomerId = new JLabel("Customer ID:", JLabel.RIGHT);
        labelCustomerId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(labelCustomerId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textCustomerId.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(textCustomerId,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(labelQuantity, gbc);

        labelProduct = new JLabel("Product 1:", JLabel.RIGHT);
        labelProduct.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(labelProduct, gbc);

        textBarcode = new JTextField();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textBarcode.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(textBarcode,gbc);

        textQuantity = new JTextField();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textQuantity.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(textQuantity,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelStartLease.add(errorMessage, gbc);

        buttonAdd = new JButton("Add more");
        buttonRemove = new JButton("Remove");
        buttonSubmit = new JButton("Submit");
        buttonClear = new JButton("Clear");
        buttonBack = new JButton("Back");

        buttonAdd.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if(textBarcode.getText().equals("") || textQuantity.getText().equals("")){
                        errorMessage.setText("Not possible!");
                        buttonAdd.setEnabled(false);
                        buttonRemove.setEnabled(false);
                        buttonSubmit.setEnabled(false);
                        buttonBack.setEnabled(false);
                    }else{
                        if (leaseId==0)
                            leaseId = leaseController.createLease(getDate(), textBarcode.getText(), Integer.parseInt(textQuantity.getText()), 
                                textPeriod.getText(), Integer.parseInt(textCustomerId.getText()), sessionId);
                        String barcode = textBarcode.getText();
                        int productBarcode = Integer.parseInt(barcode);
                        int amount = Integer.parseInt(textQuantity.getText());
                        boolean check = leaseController.addProductInLease(leaseId,productBarcode,amount);
                        if(!check) {
                            leaseController.removeProductFromLease(leaseId,productBarcode,amount);
                        }

                        count++;
                        labelProduct = new JLabel("Product " + (count+1) + ":", JLabel.RIGHT);
                        labelProduct.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelStartLease.add(labelProduct, gbc);

                        textBarcode = new JTextField();
                        gbc.gridx = 1;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textBarcode.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(textBarcode,gbc);

                        textQuantity = new JTextField();
                        gbc.gridx = 2;
                        gbc.gridy = 3 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        textQuantity.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(textQuantity,gbc);

                        errorMessage = new JLabel("", JLabel.RIGHT);
                        errorMessage.setPreferredSize(new Dimension(100, 25));
                        gbc.gridx = 0;
                        gbc.gridy = 4 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        panelStartLease.add(errorMessage, gbc);

                        gbc.gridx = 0;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonAdd.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(buttonAdd,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonRemove.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(buttonRemove,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 5 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonSubmit.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(buttonSubmit,gbc);

                        gbc.gridx = 1;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonClear.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(buttonClear,gbc);

                        gbc.gridx = 2;
                        gbc.gridy = 6 + count;
                        gbc.insets = new Insets(5,5,5,5);
                        buttonBack.setPreferredSize(new Dimension(100,25));
                        panelStartLease.add(buttonBack,gbc);

                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonAdd.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(buttonAdd,gbc);

        buttonRemove.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelStartLease.remove(labelProduct);
                    panelStartLease.remove(textBarcode);
                    panelStartLease.remove(textQuantity);
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count--;

                    gbc.gridx = 0;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonAdd.setPreferredSize(new Dimension(100,25));
                    panelStartLease.add(buttonAdd,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonRemove.setPreferredSize(new Dimension(100,25));
                    panelStartLease.add(buttonRemove,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 5 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonSubmit.setPreferredSize(new Dimension(100,25));
                    panelStartLease.add(buttonSubmit,gbc);

                    gbc.gridx = 1;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonClear.setPreferredSize(new Dimension(100,25));
                    panelStartLease.add(buttonClear,gbc);

                    gbc.gridx = 2;
                    gbc.gridy = 6 + count;
                    gbc.insets = new Insets(5,5,5,5);
                    buttonBack.setPreferredSize(new Dimension(100,25));
                    panelStartLease.add(buttonBack,gbc);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonRemove.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(buttonRemove,gbc);

        buttonSubmit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    if (leaseId==0)
                        leaseId = leaseController.createLease(getDate(), textBarcode.getText(), Integer.parseInt(textQuantity.getText()), 
                            textPeriod.getText(), Integer.parseInt(textCustomerId.getText()), sessionId);
                    String barcode = textBarcode.getText();
                    int productBarcode = Integer.parseInt(barcode);
                    int amount = Integer.parseInt(textQuantity.getText());
                    boolean check = leaseController.addProductInLease(leaseId,productBarcode,amount);
                    if(!check) {
                        leaseController.removeProductFromLease(leaseId,productBarcode,amount);
                    }

                    leaseController.endLease(leaseId);
                    leaseId=0;
                    count = 0;

                    container = getContentPane();
                    panelStartLease.removeAll();
                    container.removeAll();
                    menuLeases();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        buttonSubmit.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(buttonSubmit,gbc);

        buttonClear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textBarcode.setText("");
                    textQuantity.setText("");
                    buttonAdd.setEnabled(true);
                    buttonRemove.setEnabled(true);
                    buttonSubmit.setEnabled(true);
                    buttonBack.setEnabled(true);
                    container = getContentPane();
                    panelStartLease.removeAll();
                    container.removeAll();
                    startLease();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonClear.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(buttonClear,gbc);

        buttonBack.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelStartLease.removeAll();
                    container.removeAll();
                    menuLeases();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    count = 0;
                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        buttonBack.setPreferredSize(new Dimension(100,25));
        panelStartLease.add(buttonBack,gbc);
    }

    public void startLease(){
        menuStartLease();
        this.getContentPane().add(panelStartLease, BorderLayout.NORTH);
    }

    private void menuFinishLease(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton returnn;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFinishLease.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFinishLease.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFinishLease.add(errorMessage, gbc);

        returnn = new JButton("Return");
        returnn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        int id = Integer.parseInt(textId.getText());
                        Lease lease = leaseController.findLease(id);
                        if(lease.onTime())
                        {
                            errorMessage.setText("On time!");
                        } else {
                            errorMessage.setText("Fined");
                        }
                        lease.setReturned(false);
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findLease();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No lease!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        returnn.setPreferredSize(new Dimension(100,25));
        panelFinishLease.add(returnn,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelShowLease.removeAll();
                    container.removeAll();
                    menuLeases();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelFinishLease.add(back,gbc);
    }

    public void finishLease(){
        menuFinishLease();
        this.getContentPane().add(panelFinishLease, BorderLayout.NORTH);
    }

    private void menuShowLease(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowLease.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        showLeaseSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findLease();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No lease!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelShowLease.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelShowLease.removeAll();
                    container.removeAll();
                    menuLeases();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelShowLease.add(back,gbc);
    }

    public void findLease(){
        menuShowLease();
        this.getContentPane().add(panelShowLease, BorderLayout.NORTH);
    }

    private void showSecondWindow(int id){
        JTextField textId = new JTextField();
        JTextField textDate = new JTextField();
        JTextField textPeriod = new JTextField();
        JTextField textEmployee = new JTextField();
        JTextField textCustomer = new JTextField();
        JTextField textReturned = new JTextField();
        JLabel labelId;
        JLabel labelDate;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JLabel labelNull;
        JLabel labelPeriod;
        JLabel labelEmployee;
        JLabel labelCustomer;
        JLabel labelReturned;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textDate.setEditable(false);
        textPeriod.setEditable(false);
        textEmployee.setEditable(false);
        textCustomer.setEditable(false);
        textReturned.setEditable(false);

        textId.setText(Integer.toString(leaseController.findLease(id).getId()));
        textDate.setText(leaseController.findLease(id).getCurrentDate());
        textPeriod.setText(leaseController.findLease(id).getPeriod());
        textEmployee.setText(Integer.toString(leaseController.findLease(id).getEmployeeID()));
        textCustomer.setText(Integer.toString(leaseController.findLease(id).getCustomerID()));
        if(leaseController.findLease(id).isReturned()){
            textReturned.setText("Not Returned");
        }
        else{
            textReturned.setText("Returned");
        }

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textId,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelDate, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textDate,gbc);

        labelPeriod = new JLabel("Period:", JLabel.RIGHT);
        labelPeriod.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelPeriod, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPeriod.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textPeriod,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelQuantity, gbc);

        int y = 5;
        for(int i=0; i<LeaseContainer.getInstance().findLeaseById(id).getProducts().size(); i++){

            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelShowLease2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelShowLease2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelShowLease2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) LeaseContainer.getInstance().findLeaseById(id).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(leaseController.findLease(id).getProducts().get(key)));
        }

        labelNull = new JLabel("", JLabel.RIGHT);
        labelNull.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelNull, gbc);

        labelEmployee = new JLabel("Employee:", JLabel.RIGHT);
        labelEmployee.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelEmployee, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textEmployee.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textEmployee,gbc);

        labelCustomer = new JLabel("Customer:", JLabel.RIGHT);
        labelCustomer.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelCustomer, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textCustomer.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textCustomer,gbc);

        labelReturned = new JLabel("Returned:", JLabel.RIGHT);
        labelReturned.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelShowLease2.add(labelReturned, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textReturned.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(textReturned,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelShowLease2.removeAll();
                    container.removeAll();
                    findLease();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelShowLease2.add(ok,gbc);
    }

    private void showLeaseSecondWindow(int id){
        showSecondWindow(id);
        this.getContentPane().add(panelShowLease2, BorderLayout.NORTH);
    }

    private void deleteFirstWindow(){
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteLease.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.LEFT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteLeaseSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        deleteLeaseFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        errorMessage.setText("No lease!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteLease.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    menuLeases();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteLease.add(back,gbc);
    }

    private void deleteLeaseFirstWindow(){
        deleteFirstWindow();
        this.getContentPane().add(panelDeleteLease, BorderLayout.NORTH);
    }

    private void deleteSecondWindow(int id){
        JTextField textId = new JTextField();
        JTextField textDate = new JTextField();
        JTextField textPeriod = new JTextField();
        JTextField textEmployee = new JTextField();
        JTextField textCustomer = new JTextField();
        JTextField textReturned = new JTextField();
        JLabel labelId;
        JLabel labelDate;
        JLabel labelBarcode;
        JLabel labelQuantity;
        JLabel labelNull;
        JLabel labelPeriod;
        JLabel labelEmployee;
        JLabel labelCustomer;
        JLabel labelReturned;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textDate.setEditable(false);
        textPeriod.setEditable(false);
        textEmployee.setEditable(false);
        textCustomer.setEditable(false);
        textReturned.setEditable(false);

        textId.setText(Integer.toString(leaseController.findLease(id).getId()));
        textDate.setText(leaseController.findLease(id).getCurrentDate());
        textPeriod.setText(leaseController.findLease(id).getPeriod());
        textEmployee.setText(Integer.toString(leaseController.findLease(id).getEmployeeID()));
        textCustomer.setText(Integer.toString(leaseController.findLease(id).getCustomerID()));
        if(leaseController.findLease(id).isReturned()){
            textReturned.setText("Not Returned");
        }
        else{
            textReturned.setText("Returned");
        }

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelId, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textId,gbc);

        labelDate = new JLabel("Date:", JLabel.RIGHT);
        labelDate.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelDate, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textDate.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textDate,gbc);

        labelPeriod = new JLabel("Period:", JLabel.RIGHT);
        labelPeriod.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelPeriod, gbc);

        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPeriod.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textPeriod,gbc);

        labelBarcode = new JLabel("Barcode:", JLabel.LEFT);
        labelBarcode.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelBarcode, gbc);

        labelQuantity = new JLabel("Quantity:", JLabel.LEFT);
        labelQuantity.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelQuantity, gbc);

        int y = 5;
        for(int i=0; i<LeaseContainer.getInstance().findLeaseById(id).getProducts().size(); i++){

            int productNumber = i;
            labelProduct = new JLabel("Product "+ ++productNumber +":", JLabel.RIGHT);
            labelProduct.setPreferredSize(new Dimension(100, 25));
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            panelDeleteLease2.add(labelProduct, gbc);

            textBarcode = new JTextField();
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textBarcode.setPreferredSize(new Dimension(100,25));
            panelDeleteLease2.add(textBarcode,gbc);

            textQuantity = new JTextField();
            gbc.gridx = 2;
            gbc.gridy = y;
            gbc.insets = new Insets(5,5,5,5);
            textQuantity.setPreferredSize(new Dimension(100,25));
            panelDeleteLease2.add(textQuantity,gbc);
            y++;

            textBarcode.setEditable(false);
            textQuantity.setEditable(false);
            int key = (int) LeaseContainer.getInstance().findLeaseById(id).getProducts().keySet().toArray()[i];
            textBarcode.setText(Integer.toString(key));
            textQuantity.setText(Integer.toString(leaseController.findLease(id).getProducts().get(key)));
        }

        labelNull = new JLabel("", JLabel.RIGHT);
        labelNull.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 2;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelNull, gbc);

        labelEmployee = new JLabel("Employee:", JLabel.RIGHT);
        labelEmployee.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelEmployee, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textEmployee.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textEmployee,gbc);

        labelCustomer = new JLabel("Customer:", JLabel.RIGHT);
        labelCustomer.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelCustomer, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textCustomer.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textCustomer,gbc);

        labelReturned = new JLabel("Returned:", JLabel.RIGHT);
        labelReturned.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 1;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteLease2.add(labelReturned, gbc);

        gbc.gridx = 2;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        textReturned.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(textReturned,gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    leaseController.deleteLease(id);
                    container = getContentPane();
                    container.removeAll();
                    deleteLeaseFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = ++y;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelDeleteLease2.removeAll();
                    container.removeAll();
                    deleteLeaseFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = y;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteLease2.add(cancel, gbc);
    }

    private void deleteLeaseSecondWindow(int id){
        deleteSecondWindow(id);
        this.getContentPane().add(panelDeleteLease2, BorderLayout.NORTH);
    }

    private String getDate(){
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
        Date date = new Date();
        String dateString = dateFormat.format(date);
        return dateString;
    }
}
